<?php
include ('lib/jpgraph/src/jpgraph.php');
include ('lib/jpgraph/src/jpgraph_bar.php');

function colorized($p, $fd = NULL) {
	return $fd === true ? 'red' : (
		$p < 50 ? 'white' : (
			$p < 80 ? 'lightgray' : (
				$p <= 100 ? 'lightgreen' : (
					$p < 110 ? 'orange' : 'red'
				)
			)
		)
	);
}

// Get data from client
$w = isset($_REQUEST['w']) ? $_REQUEST['w'] : 320;
$h = isset($_REQUEST['h']) ? $_REQUEST['h'] : 240;
$t = isset($_REQUEST['t']) ? str_replace('_', ' ', $_REQUEST['t']) : 'Bar chart';
$x = isset($_REQUEST['d']) ? explode(',', $_REQUEST['d']) : array();
$y = isset($_REQUEST['u']) ? explode(',', $_REQUEST['u']) : array();
$f = isset($_REQUEST['f']) ? explode(',', $_REQUEST['f']) : array();
$c = array();
foreach ($y as $i => $v) {
	$c[] = colorized($v, $f[$i] == 0).'@0.30';
}

// Create the graph. 
$graph = new Graph($w, $h);
$graph->SetScale('textlin');
$graph->SetMarginColor('white');

// Adjust the margin slightly so that we use the 
// entire area (since we don't use a frame)
$graph->SetMargin(30,1,20,5);

// Box around plotarea
$graph->SetBox(); 

// No frame around the image
$graph->SetFrame(false);

// Setup the tab title
$graph->tabtitle->Set('  '.$t.'  ');
$graph->tabtitle->SetFont(FF_ARIAL,FS_BOLD,12);

// Setup the X and Y grid
$graph->ygrid->SetFill(true,'#FEFEFE@0.5','#EEEEEE@0.5');
$graph->ygrid->SetLineStyle('dashed');
$graph->ygrid->SetColor('gray');
$graph->xgrid->Show();
$graph->xgrid->SetLineStyle('dashed');
$graph->xgrid->SetColor('gray');

// Setup $x as labels on the X-axis
$graph->xaxis->SetTickLabels($x);
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,7);
$graph->xaxis->SetLabelAngle(60);

$graph->yaxis->SetFont(FF_ARIAL,FS_NORMAL,7);
$graph->yaxis->SetLabelFormat('%d %%');

// Create a bar pot
$bplot = new BarPlot($y);
$bplot->SetWidth(0.6);
$bplot->SetFillColor($c);
$bplot->SetWeight(1);
$bplot->SetShadow('gray@0.25', 2, 2);

$graph->Add($bplot);

$graph->Stroke();
?>
